<template>
	<!-- #ifndef MP-TOUTIAO -->

	<view class="head-custom-wrap" v-if="isHead">
		<template v-if="isSpace">
			<view class="spaceBar" :style="{ height: statusBarHeight + 'px' }"></view>
			<view class="spaceHead"></view>
		</template>
		<!-- <view class="head-custom head-fixed" :class="[isApp ? 'head-app': 'head-transparent']"> -->
		<view
			class="head-custom head-fixed"
			:class="{
				'head-app': isApp,
				'head-transparent': !isApp,
				'bg-none': !isBg,
				'head-circle': isCircle,
				'head-Blur': isBlur,
				'head-bg': isActive
			}"
			:style="headSyle"
		>
		<!-- isSpace -->
			<view class="statusBarHeight" :style="{ height: statusBarHeight + 'px' }"></view>
			<view class="head fboxRow Ycenter">
				<!-- #ifdef H5 -->
				<view class="left fboxRow Ycenter" v-if="isBack || title || isChange">
					<view class="inner fboxRow Ycenter">
						<view class="action-btn fboxRow Ycenter Xcenter" @click="goHome" v-if="isChange && isHome"><text class="bwfont bw-a-zu1221"></text></view>
						<view class="action-btn fboxRow Ycenter" @click="backTap" v-else style="padding: 0 20rpx;"><text v-if="isBack" class="bwfont bw-fanhui"></text></view>
						<!-- <view class="font38 color-E8CDA7 title" @click="backTap" v-if="title">{{ title }}</view> -->
					</view>
				</view>
				<view class="flex1">
					<view class="font38 fboxRow Ycenter Xcenter title" v-if="title">{{ title }}</view>
				</view>
				<view class="right fboxRow Ycenter Xend">
					<view v-if="!isApp && isAppBtn" class="btn-app fboxRow Ycenter Xcenter font26 bg-linear" @click="openApp">打开APP</view>
					<slot name="right"></slot>
					<template v-if="!isApp && isHome && !isChange">
						<view class="action-btn home-action fboxRow Ycenter Xcenter" @click="goHome"><text class="bwfont bw-a-zu1221"></text></view>
					</template>
				</view>
				<!-- #endif -->

				<!-- #ifdef MP -->
				<view class="left fboxRow Ycenter wx-left">
					<template v-if="type == 'index'">
						<slot name="left"></slot>
					</template>
					<template v-else>
						<view class="inner-wx fboxRow Ycenter Xcenter" :class="{'inner-wx-left': !isBack }" v-if="isBack || isHome">
							<view class="action-btn flex1 fboxRow Ycenter Xcenter" v-if="isBack" @click="backTap"><text class="bwfont bw-fanhui"></text></view>
							<view v-if="isBack && isHome" class="line"></view>
							<view v-if="isHome" class="action-btn flex1 home-action fboxRow Ycenter Xcenter" @click="goHome"><text class="bwfont bw-a-zu1221"></text></view>
						</view>
					</template>
				</view>
				<view class="flex1">
					<view class="fboxRow Ycenter Xcenter inner-wx-center font38 title" :class="!isBack ? 'left-title' : ''" v-if="title">{{ title }}</view>
				</view>
				<view class="right fboxRow Ycenter Xend"><view class="inner-wx-right" v-if="isBack || isHome"></view></view>
				<!-- #endif -->
			</view>
		</view>
		<ModelPublic ref="ModelPublicRef" :isShowClose="false" title="提示" @confirm="confirmClose">
			<template #msg>
				<view class="model-msg">当前内容未保存，是否退出？</view>
			</template>
		</ModelPublic>
	</view>

	<!-- #endif -->
</template>

<script>
import { getAndroidVersion } from '@/api/index.js';
import { getRequest } from '@/static/js/common.js';
import { base64decode, base64encode, utf8to16 } from '@/static/js/Base64.js';
import { openAppULink, openAppULinkNew } from '@/static/js/common.js';
import ModelPublic from '@/components/model-public/index.vue';
export default {
	name: 'head-custom',
	components: { ModelPublic },
	props: {
		headSyle:{
			type: String,
			default: '' // 头部颜色单独修改
		},
		isActive: {
			type: Boolean,
			default: false // 红包活动
		},
		type: {
			type: String,
			default: '' // 导航类型
		},
		title: {
			type: String,
			default: '' // 标题
		},
		isBg: {
			// 是否有背景
			type: Boolean,
			default: true
		},
		isChange: {
			// 返回和首页按钮交换
			type: Boolean,
			default: false
		},
		isBlur: {
			// 是否虚化
			type: Boolean,
			default: true
		},
		isSpace: {
			type: Boolean,
			default: true //占位
		},
		isCircle: {
			type: Boolean,
			default: false // 透明 按钮灰背景
		},
		isHome: {
			type: Boolean,
			default: true // 是否显示主页
		},
		isBack: {
			type: Boolean,
			default: true // 是否显示返回按钮
		},
		isAppBtn: {
			type: Boolean,
			default: true // 是否显示打开APP
		},
		linkData: {
			type: Object,
			default: () => {
				return {}; // 友盟超链增加参数
			}
		},
		linkId: {
			type: String,
			default: 'usr103rs12t9nk1c' // 友盟超链对应分享key
		},
		isOpeninstall: {
			type: Boolean,
			default: false
		}
	},
	data() {
		return {
			sys: {},
			statusBarHeight: getApp().globalData.statusBarHeight,
			isApp: false,
			urlAndroid: '',
			timer: null,
			isHead: true,
			wxLeft: ''
		};
	},
	computed: {
		_linkData: {
			get() {
				return this.linkData;
			},
			set(val) {
				this.$emit('update:linkData', val);
			}
		}
	},
	created() {
		// #ifdef H5
		// 引入深链js u-link友盟，openinstall
		const script = window.document.createElement("script");
		script.type = "text/javascript";
		script.src = "https://g.alicdn.com/jssdk/u-link/index.min.js";
		document.getElementsByTagName("head")[0].appendChild(script);
		const script2 = window.document.createElement("script");
		script2.type = "text/javascript";
		script2.src = "https://web.cdn.openinstall.io/openinstall.js";
		document.getElementsByTagName("head")[0].appendChild(script2);
		this.$bridge.initHandler((message, responseCallback) => {
			const responseData = '初始化收收到来自Java的数据，回传数据给你';
			responseCallback(responseData);
		});
		// #endif
		this.statusBarHeight = getApp().globalData.statusBarHeight;
		if (!getApp().globalData.statusBarHeight) {
			const sys = uni.getSystemInfoSync();
			this.statusBarHeight = sys.statusBarHeight;
		}
		// #ifdef MP
		// 根据胶囊按钮距离， 设置导航高度
		const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
		this.statusBarHeight = menuButtonInfo.top;
		// #endif
	},
	mounted() {
		const sys = uni.getSystemInfoSync();
		this.sys = sys;
		// #ifdef H5
		this.getAndroidUrl();
		const { bw_link, bw_params } = getApp().getBWData(['bw_link', 'bw_params']);
		this.isApp = bw_link.isApp == 1 ? true : false;
		this.isHead = bw_link.isHead == 1 ? false : true;
		this.statusBarHeight = bw_params.statusBarHeight;
		// #endif
	},
	destroyed() {
		clearTimeout(this.timer);
	},
	methods: {
		// 确定关闭
		confirmClose() {
			// #ifdef H5
			uni.navigateBack({
				delta: 1,
				success: res => {
					this.timer = setTimeout(() => {
						const data = {
							type: 'back'
						};
						this.$bridge.callHandler('recruitHandler', data, () => {
							console.log('recruitHandler', '返回');
						});
						// #ifdef MP
						wx.miniProgram.navigateBack({})
						 uni.postMessage({
							data: { message: '我是来自H5的消息' }
						})
						// #endif
					}, 500);
				},
				fail: () => {}
			});
			// #endif
			// #ifdef MP
			uni.navigateBack();
			// #endif
			this.$refs.ModelPublicRef.show = false;
		},
		backTap() {
			if (!this.isBack) {
				return;
			}
			const pages = getCurrentPages();
			if (pages[pages.length - 1].route == 'bwSign/createQd') {
				this.$refs.ModelPublicRef.show = true;
				return;
			}
			// #ifdef H5
			uni.navigateBack({
				delta: 1,
				success: res => {
					this.timer = setTimeout(() => {
						const data = {
							type: 'back'
						};
						this.$bridge.callHandler('recruitHandler', data, () => {
							console.log('recruitHandler', '返回');
						});
					}, 500);
				},
				fail: () => {}
			});
			// #endif
			// #ifdef MP
			uni.navigateBack();
			// #endif
		},
		goHome() {
			// #ifdef H5
			this.$pageTo('/index/index', '');
			// #endif
			// #ifdef MP
			this.$pageTo('/pages/index/index', 'reLaunch');
			// this.$pageTo('/personal/index')
			// #endif
		},
		// 获取当前安卓下载地址的URL
		getAndroidUrl() {
			const bw_android_version = sessionStorage.getItem('bw_android_version')
			if(bw_android_version){
				const data= JSON.parse(bw_android_version)
				this.urlAndroid = data.updateAddress;
			}else{
				getAndroidVersion().then(res => {
					sessionStorage.setItem('bw_android_version', JSON.stringify(res.data))
					this.urlAndroid = res.data.updateAddress;
				});
			}
		},
		openApp() {
			const { bw_link, bw_user_temp } = getApp().getBWData(['bw_link', 'bw_user_temp']);
			const href = window.location.href;
			let share = getRequest(href, 'bwlink');
			let sharerId = bw_link.sharerId;
			if (share) {
				share = decodeURIComponent(share);
				const data = JSON.parse(utf8to16(base64decode(share)));
				sharerId = data.sharerId;
				console.log(data);
			}
			const obj = {
				...this._linkData,
				sharerId,
				href: href
				// href: href.substr(0, href.indexOf("?")),
			};
			if (obj.realName) {
				obj.realName = encodeURIComponent(obj.realName);
			}
			if (obj.companyName) {
				obj.companyName = encodeURIComponent(obj.companyName);
			}
			const url = this.isOpeninstall ? openAppULinkNew : openAppULink;
			url(this.linkId, obj, this.urlAndroid);
		}
	}
};
</script>

<style lang="scss" scoped>
@import url('./index.scss');
// #ifdef MP
.head-transparent .head {
	padding-left: 20rpx;
}
.title {
	bottom: 10rpx;
}
// #endif


</style>
